package com.ruoyi.ldzlsc.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ldzlsc.domain.vo.WorkOrderVO;
import com.ruoyi.ldzlsc.domain.vo.ScheduleRequestVO;
import com.ruoyi.ldzlsc.service.IScWorkOrderService;
import com.ruoyi.ldzlsc.service.IScWorkOrderTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 工单Controller
 *
 * @author ruoyi
 * @date 2024-07-31
 */
@RestController
@RequestMapping("/workOrder")
public class ScWorkOrderController extends BaseController {
    @Autowired
    private IScWorkOrderService workOrderService;

    @Autowired
    private IScWorkOrderTaskService workOrderTaskService;

    /**
     * 查询工单列表
     */
    //@RequiresPermissions("sc:workOrder:list")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrderVO workOrderVO) {
        startPage();
        List<WorkOrderVO> list = workOrderService.selectWorkOrderList(workOrderVO);
        return getDataTable(list);
    }
    
    /**
     * 导出工单列表
     */
    @Log(title = "工单管理", businessType = BusinessType.EXPORT)
    //@RequiresPermissions("sc:workOrder:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkOrderVO workOrderVO) {
        List<WorkOrderVO> list = workOrderService.selectWorkOrderList(workOrderVO);
        ExcelUtil<WorkOrderVO> util = new ExcelUtil<WorkOrderVO>(WorkOrderVO.class);
        util.exportExcel(response, list, "工单数据");
    }

    /**
     * 获取工单详细信息
     */
    //@RequiresPermissions("sc:workOrder:query")
    @GetMapping(value = "/{workOrderId}")
    public AjaxResult getInfo(@PathVariable("workOrderId") Long workOrderId)
    {
        return success(workOrderService.selectScWorkOrderById(workOrderId));
    }
    
    /**
     * 开始工单
     */
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    //@RequiresPermissions("sc:workOrder:start")
    @PutMapping("/start/{workOrderId}")
    public AjaxResult start(@PathVariable("workOrderId") Long workOrderId) {
        return toAjax(workOrderService.startWorkOrder(workOrderId, SecurityUtils.getUsername()));
    }
    
    /**
     * 取消工单
     */
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    //@RequiresPermissions("sc:workOrder:cancel")
    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody Long[] workOrderIds) {
        return toAjax(workOrderService.cancelWorkOrder(workOrderIds, SecurityUtils.getUsername()));
    }
    
    /**
     * 结束工单
     */
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    //@RequiresPermissions("sc:workOrder:finish")
    @PutMapping("/finish/{workOrderId}")
    public AjaxResult finish(@PathVariable("workOrderId") Long workOrderId) {
        return toAjax(workOrderService.finishWorkOrder(workOrderId, SecurityUtils.getUsername()));
    }
    
    /**
     * 撤回工单
     */
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    //@RequiresPermissions("sc:workOrder:withdraw")
    @PutMapping("/withdraw/{workOrderId}")
    public AjaxResult withdraw(@PathVariable("workOrderId") Long workOrderId) {
        return toAjax(workOrderService.withdrawWorkOrder(workOrderId, SecurityUtils.getUsername()));
    }
    
    /**
     * 保存排产计划
     */
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    //@RequiresPermissions("sc:workOrder:schedule")
    @PutMapping("/schedule")
    public AjaxResult schedule(@RequestBody ScheduleRequestVO requestVO)
    {
        workOrderTaskService.saveSchedule(requestVO.getWorkOrderId(), requestVO.getTasks());
        
        return success("排产成功");
    }
} 